package com.abe.libquick.widget.dialog;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.abe.libcore.view.common.BaseJDialog;
import com.abe.libquick.R;
import com.abe.libquick.utils.quick.ToastTool;

public class ServerSetDialog extends BaseJDialog implements View.OnClickListener {

    private onServerSetCallback callback;
    private String serverName, serverUrl;
    private EditText itemUrl;
    public ServerSetDialog(@NonNull Context context) {
        super(context);
    }

    public ServerSetDialog(@NonNull Context context, String serverName, String serverUrl, onServerSetCallback callback) {
        super(context);
        this.serverName = serverName;
        this.serverUrl = serverUrl;
        this.callback = callback;
    }

    @Override
    protected int getLayoutRes() {
        return R.layout.item_view_server_set;
    }

    @Override
    protected void initContent() {
        itemUrl = findViewById(R.id.item_url);
        TextView itemName = findViewById(R.id.item_name);
        itemName.setText(serverName);
        itemUrl.setText(serverUrl);
        findViewById(R.id.item_cancel).setOnClickListener(this);
        findViewById(R.id.item_confirm).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.item_cancel) {
            dismiss();
            if (callback != null) {
                callback.onServerSet(false, null);
            }
        } else if (id == R.id.item_confirm) {
            String url = itemUrl.getText().toString().trim();
            if (TextUtils.isEmpty(url)) {
                ToastTool.show("服务地址不能为空");
                return;
            }
            dismiss();
            if (callback != null) {
                callback.onServerSet(true, url);
            }
        }
    }

    public interface onServerSetCallback {
        void onServerSet(boolean isChange, String url);
    }
}